package org.jeecg.modules.scm.controller;

import cfca.trustsign.demo.op.CfcaOp;
import io.seata.spring.annotation.GlobalTransactional;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.CommonAPI;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.constant.CommonConstant;
import org.jeecg.common.util.CommonUtils;
import org.jeecg.common.util.RedisUtil;
import org.jeecg.modules.flow.api.FlowBaseCloudApi;
import org.jeecg.modules.flow.entity.FlowAudit;
import org.jeecg.modules.imm.api.ImmBaseCloudApi;
import org.jeecg.modules.scm.entity.ScmInvestor;
import org.jeecg.modules.scm.service.IJeecgHelloService;
import org.jeecg.modules.scm.service.IScmInvestorService;
import org.jeecg.modules.scm.service.impl.ScmFinaApplyServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.InputStream;


@RestController
@RequestMapping("/test/jeecg")
@Api(tags = "测试")
@Slf4j
public class JeecgHelloController {

	@Lazy
	@Resource
	private CommonAPI commonAPI;

	@Autowired
	private IJeecgHelloService jeecgHelloService;

	@Autowired
	private FlowBaseCloudApi flowBaseCloudApi;

	@Autowired
	private IScmInvestorService scmInvestorService;
	@Autowired
	private ScmFinaApplyServiceImpl scmFinaApplyService;

	@Autowired
	@Lazy
	private RedisUtil redisUtil;

	@Autowired
	private ImmBaseCloudApi immBaseCloudApi;




	@ApiOperation(value = "接口测试")
	@GetMapping(value = "/hello")
	public String queryPageList(@RequestParam String name) {
//		LoginUser loginUser = commonAPI.getUserByName("prb");
//		TaskQuery query = new TaskQuery();
//		query.setBusinessKey(null);
//		query.setProcessInstanceId(null);
//		ProInstanceVo proInstance = flowBaseCloudApi.searchInstanceById("251976e9-4e62-11ec-9644-0a0027000005").getResult();
//		System.out.println(proInstance);
//		String cfca = scmFinaApplyService.checkApply(17).getCfca();
//		System.out.println("cfca:"+cfca);
//		String contract = (String) redisUtil.get(cfca);
//
//		JSONObject parseObject = JSONObject.parseObject(contract);
//		UploadContractVO contractVO = JSON.toJavaObject(parseObject, UploadContractVO.class);
//		MultiValueMap<String, Object> params = new LinkedMultiValueMap<>();
//		params.add("data",1212);
//		immBaseCloudApi.finaApplySend("http://localhost:9999/jeecg-scm/test/jeecg/test",params);
		//测试更新合同
		String path = "test/contractTemplate/21.pdf";
		String fileName = path.substring(path.lastIndexOf('\\')>=0?path.lastIndexOf('\\'):path.lastIndexOf('/') + 1);
		InputStream inputStream = CfcaOp.downloadContract("MM20211213000000561");
		String upload = CommonUtils.upload(inputStream,
				fileName,
				"/contractTemplate",
				CommonConstant.UPLOAD_TYPE_MINIO);
		return upload;
	}

	@PostMapping(value="/test")
	public String  test(HttpServletRequest req){
		System.out.println("收到：" + req.getParameter("data"));

		return "接收成功";
	}

	@GetMapping(value = "/test2")
	public String test2(HttpServletRequest req) {
		//System.out.println("收到："+req.getParameter("data"));
		System.out.println("32113");
		return "接收成功";
	}

	@ApiOperation("事务测试")
	@GetMapping("/transactional")
	@GlobalTransactional(rollbackFor = Exception.class)
	public Result<?> transactional(Long time) throws Exception {
		FlowAudit flowAudit = new FlowAudit();
		flowAudit.setAuditInfo("info");
		flowAudit.setTaskId("sss");
		Result<?> result = flowBaseCloudApi.add(flowAudit);
		log.info("休眠");
		Thread.sleep(time);
		ScmInvestor scmInvestor = new ScmInvestor();
		scmInvestor.setBankNo(55);
		scmInvestor.setType(1);
		scmInvestorService.save(scmInvestor);
		log.info(result.toString());
		return result;
	}

}
